﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Win32;
using ProjectWizardManagement.Entity.Model;
using ProjectWizardManagementv1.Infrastructure.Concrete;
using ProjectWizardManagementv1.Infrastructure.Static;
using RelayCommand = GalaSoft.MvvmLight.Command.RelayCommand;

namespace ProjectWizardManagementv1.ViewModels.Concrete
{
    public class ProjectDialogViewModel : ViewModelBase
    {
        #region Properties

        #endregion

        #region Members
        #endregion

        #region RelayCommandProperties

        public RelayCommand CreateProject { get; private set; }
        public RelayCommand OpenProject { get; private set; }

        #endregion

        #region Constructor
            public ProjectDialogViewModel()
            {
                CreateProject = new RelayCommand(CreateProjectExecuted);
                OpenProject = new RelayCommand(OpenProjectExecuted);
            }
        #endregion

        private void CreateProjectExecuted()
        {
            var dialog = new SaveFileDialog
                                {
                                    FileName = "",
                                    DefaultExt = ".projwm",
                                    Filter = "Project Wizard Management (.projwm)|*.projwm"
                                };

            if(dialog.ShowDialog().HasValue)
            {
                string path = dialog.FileName.Replace(".projwm", "");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var project = new Project
                                      {
                                          ProjectDirectory = path + "\\",
                                          Name = dialog.SafeFileName
                                      };
                new XmlIO().SerializeToOutput(string.Format(@"{0}\\{1}",path, dialog.SafeFileName), project);
                Messenger.Default.Send(project);
                Messenger.Default.Send(new List<string>{dialog.SafeFileName}.ToArray());
                Messenger.Default.Send(ViewModelKey.Workspace);
            }
        }

        private void OpenProjectExecuted()
        {
            var dialog = new OpenFileDialog
                             {
                                 FileName = "",
                                 DefaultExt = ".projwm",
                                 Filter = "Project Wizard Management (.projwm)|*.projwm"
                             };
            bool? result = dialog.ShowDialog();
                                                                   
            if(result != null && result.Value)
            {
                var project = new XmlIO().DeserializeFromInput(dialog.FileName);
                string path = Path.GetDirectoryName(dialog.FileName);
                if(path != null)
                {
                    var files = FileSystemFactory.ConstructFileHierarchy(MyDirectory.EnumerateFilesAndDirectories(path));
                    Messenger.Default.Send(project);
                    Messenger.Default.Send(files.ToArray());
                    Messenger.Default.Send(ViewModelKey.Workspace);    
                }

            
            }
            else
            {
                //Show Possible Error
            }
        }
    }
}
